﻿package com.zhuangyzh.cmpp;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
/**
 *    对submitresp消息进行处理，其中msgid的解析使用了最普通的方法，目的是使SP开发者理解msgid的生成与解析
 * @author zhuangyzh
 *
 */

public class CMPPSubmitResp extends CMPPHeader {
    
    private long msgId;
    private String msgIdstr;
    private int result;// 结果 0：正确 1：消息结构错 2：命令字错 3：消息序号重复 4：消息长度错 5：资费代码错
                       // 6：超过最大信息长 7：业务代码错 8：流量控制错 9：本网关不负责服务此计费号码 10：Src_Id错误
                       // 11：Msg_src错误 12：Fee_terminal_Id错误
                       // 13：Dest_terminal_Id错误  145：超速（东软网关自定义）
    private String resultstr;// 结果 0：正确 1：消息结构错 2：命令字错 3：消息序号重复 4：消息长度错 5：资费代码错                    
                             // 6：超过最大信息长 7：业务代码错 8：流量控制错 9：本网关不负责服务此计费号码
                             // 10：Src_Id错误
                             // 11：Msg_src错误 12：Fee_terminal_Id错误
                             // 13：Dest_terminal_Id错误 145：超速（东软网关自定义）
    
    public CMPPSubmitResp(byte[] data) {
        if (data.length == 8 + 8 + 4) {
            ByteArrayInputStream bins = new ByteArrayInputStream(data);
            DataInputStream dins = new DataInputStream(bins);
            try {
                this.setTotal_Length(data.length + 4);
                this.setCommand_Id(dins.readInt());
                this.setSequence_Id(dins.readInt());
                this.msgId = dins.readLong();
                this.result = dins.readInt();
                this.setResult(result);
                this.setMsgIdStr(msgId);
                this.getMsgId(msgId);
                dins.close();
                bins.close();
                System.out.println("收到CMPPSubmitResp消息 MSGID：" + this.getMsgIdStr()
                        + " 序号：" + this.getSequence_Id() + "响应状态：" + resultstr);
            } catch (IOException e) {
            }
        } else {
            System.out.println("发送短信IMSP回复,解析数据包出错，包长度不一致。长度为:" + data.length);
        }
    }

    public long getMsgId(long msgId) {
        this.msgId = msgId;
        return msgId;
    }

    public void setMsgId(long msgId) {
        this.msgId = msgId;
    }

    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
        switch (result) {
        case 0:
            resultstr = "正确";
            break;
        case 1:
            resultstr = "消息结构错";
            break;
        case 2:
            resultstr = "命令字错";
            break;
        case 3:
            resultstr = "消息序号重复";
            break;
        case 4:
            resultstr = "消息长度错";
            break;
        case 5:
            resultstr = "资费代码错";
            break;
        case 6:
            resultstr = "超过最大信息长";
            break;
        case 7:
            resultstr = "业务代码错";
            break;
        case 8:
            resultstr = "流量控制错";
            break;
        case 9:
            resultstr = "本网关不负责服务此计费号码";
            break;
        case 10:
            resultstr = "Src_Id错误";
            break;
        case 11:
            resultstr = "Msg_src错误";
            break;
        case 12:
            resultstr = "Fee_terminal_Id错误";
            break;
        case 13:
            resultstr = "Dest_terminal_Id错误";
            break;
        default:
            resultstr = result + "请咨询运营商";
            break;
        }
    }
    //返回字符串类型的MSGID
    
    public String setMsgIdStr(long msgId) {
        this.msgId = msgId;
        //将long类型的msgid转换为二进制无符号整数的字符串形式
        String tmp = "";
        tmp=Long.toBinaryString(msgId);
        //不足64位前补0操作
        if (tmp.length() < 64) {
            String tmp0 = "00000000";
            tmp = tmp0.substring(0, 64 - tmp.length()) + tmp;
        }
        //System.out.println(tmp);
        
        //对二进制串进行分割后得到最终需要的字符串
        int month = Integer.parseInt(tmp.substring(0, 4), 2);
        int day = Integer.parseInt(tmp.substring(4, 9), 2);
        int hour = Integer.parseInt(tmp.substring(9, 14), 2);
        int min = Integer.parseInt(tmp.substring(14, 20), 2);
        int sec = Integer.parseInt(tmp.substring(20, 26), 2);
        int gatewaycode = Integer.parseInt(tmp.substring(26, 48), 2);
        int seq = Integer.parseInt(tmp.substring(48), 2);
        //按照固定格式生成字符串格式的标识
        String monthstr=String.format("%02d",month);
        String daystr=String.format("%02d",day);
        String hourstr=String.format("%02d",hour);
        String minstr=String.format("%02d",min);
        String secstr=String.format("%02d",sec);
        //因移动全国各省网关编码为5位整数 例如辽宁是02405 故此处设置为5位显示
        String gatewaycodestr=String.format("%05d",gatewaycode);
        //序列号为2字节最大值为65535 故设置为5位
        String seqstr=String.format("%05d",seq);
        msgIdstr=monthstr+daystr+hourstr+minstr+secstr+gatewaycodestr+seqstr;
        return msgIdstr;
        
    }
    
    public String getMsgIdStr(){
        return msgIdstr;
    }
}